package yc.gg.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yc.gg.domain.dto.AnimalsDTO;
import yc.gg.mapper.AnimalsMapper;
import yc.gg.domain.po.Animals;
import yc.gg.domain.req.AnimalsReq;
import yc.gg.service.IAnimalsService;
import yc.gg.utils.Result;
import yc.gg.utils.UIDS;
import yc.gg.utils.UserHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * \* User: Fuyue
 * \* Date: 2025/6/4
 * \* Description:
 * \
 */
@Service
public class AnimalsService implements IAnimalsService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AnimalsMapper animalsMapper;

    @Override
    @Transactional
    public Result addAnimals(AnimalsReq animalsReq) {
        String userId = UserHolder.getUser().getOpenid();
        if(animalsReq.getName() == null || animalsReq.getDescription()== null || animalsReq.getImageUrl() ==null){
            return Result.error("请输入流浪动物的完整信息");
        }

        UIDS uid = new UIDS();
        Animals animals = new Animals();
        BeanUtil.copyProperties(animalsReq,animals);
        animals.setId(uid.GetUIDS("CW"));

        animalsMapper.addAnimals(animals);
        animalsMapper.addAnimalsAndUser(animals.getId(),userId);

        return Result.success("发布成功");
    }

    @Override
    public Result pageSelect(int pageNum) {
        int pageSize = 6;
        List<AnimalsDTO> animals = animalsMapper.pageSelect((pageNum - 1) * pageSize, pageSize);
        return Result.success(animals);
    }

    @Override
    public Result getThis(String id) {
        Animals animal = animalsMapper.getThis(id);
        return Result.success(animal);
    }
}